<?php
/**
 * 订单支付成功回调抽象类
 */
namespace app\common\model\order;
use app\common\model as M;

abstract class OrderAbtract extends \app\common\model\Order
{
   /** 订单支付成功回调 */
   abstract public function paid_callback( $order_info );

   /**
    * 创建订单
    * @param    array            $data [description]
    * @return   [type]                 [description]
    */
   public static function create_order ( array $data )
   {
   	$order_outer_id = $data['order_outer_id'] ?? 0;
   	if ( empty($order_outer_id) ) exception('order_outer_id值必传');

   	$uid = $data['uid'] ?? 0;
   	if ( empty($uid) ) exception('uid值必传');

   	$order_type = static::get_order_type();

		$order = [
			'order_no'       => \app\common\model\Order::createOrderNo(static::get_order_no_prefix()),
			'orgn_amt'       => $data['orgn_amt'] ?? 0,
			'disc_amt'       => $data['disc_amt'] ?? 0,
			'order_amt'      => $data['order_amt'] ?? 0,
			'integral'       => $data['integral'] ?? 0,
			'golds'          => $data['golds'] ?? 0,
			'order_outer_id' => $order_outer_id,
			'create_time'    => date('Y-m-d H:i:s'),
			'uid'            => $uid,
			'order_type'     => $order_type,
			'spread_suid'    => $data['spread_suid'] ?? (int)M\User::where('uid',$uid)->value('spread_suid'),
			'spread_uid'     => $data['spread_uid'] ?? (int)M\User::where('uid',$uid)->value('spread_uid'),
			'remark'         => $data['remark'] ?? '',
			'status'         => 0,
			'pay_type'       => $data['pay_type'] ?? 1,
			'description'    => $data['description'] ?? '',
			'use_deposit'    => $data['use_deposit'] ?? 0,
			'deposit_amt'    => $data['deposit_amt'] ?? 0,
			'final_amt'      => $data['final_amt'] ?? 0,
		];

		$isUpdate = false;
		$isExist = M\Order::where(['order_type'=>$order_type,'order_outer_id'=>$order_outer_id])->find();
		if ( $isExist ){
			$isUpdate = true;
			$order['id'] = $isExist->id;
		}

   	$obj = new \app\common\model\Order();

		$res = $obj->isUpdate($isUpdate)->save($order);
		if ( $res === false ) throw new \Exception('订单创建失败【1001】');
		
		## 记录订单日志
		M\OrderLogs::create([
			'order_id'    => $obj->id,
			'uid'         => $obj->uid,
			'status'      => $obj->status,
			'create_time' => $obj->create_time,
			'remark'      => $isUpdate ? '订单更新成功' : '订单创建成功'
		]);
		
		## 是否使用定金，如果是则需分别生成定金子订单和尾款子订单
		if ( $obj->use_deposit == 1 ) {
			$depositOrder = M\OrderSubs::saveOne([
				'order_id'   => $obj->id,
				'order_type' => 1,
				'order_no'   => $obj->order_no . '-1',
				'order_amt'  => $obj->deposit_amt,
			]);
			if ( $depositOrder === false ) throw new \Exception('定金子订单创建失败【1002】');

			$finalOrder = M\OrderSubs::saveOne([
				'order_id'   => $obj->id,
				'order_type' => 2,
				'order_no'   => $obj->order_no . '-2',
				'order_amt'  => $obj->final_amt,
			]);
			if ( $finalOrder === false ) throw new \Exception('尾款子订单创建失败【1002】');
		}

		return static::after_create_order($obj , $data);
   }

   /** 获取订单编号前缀 */
   protected static function get_order_no_prefix()
   {
      return '';
   }

   /** 获取订单类型 */
   protected static function get_order_type()
   {
      return '';
   }

   /** 订单创建后 */
   protected static function after_create_order($orderObj , array $data)
   {
      return $orderObj;
   }
} //Class End